package com.fpf.springboot.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fpf.springboot.domain.Music;
import com.fpf.springboot.mapper.MusicMapper;
import com.fpf.springboot.service.MusicService;

import redis.clients.jedis.JedisCluster;
@Service
public class MusicServiceImpl implements MusicService{
	@Autowired
	private MusicMapper musicMapper;
	//注入jedis集群对象
	@Autowired
	private JedisCluster jedisCluster;
	/**
	 * 单体缓存
	 */
	//@Cacheable(value="music")//存储在redis的缓存中，music.all作为key,List<Music>作为value
	public List<Music> findAllMusics() {
		List<Music> musics = new ArrayList<>();
		if(jedisCluster.exists("musics")) {
			String value = jedisCluster.get("musics");
			System.out.println(value);
			musics = JSON.parseArray(value, Music.class);
		}else {
			System.out.println("redis集群没有musics，经过findAllMusics方法");
			musics = musicMapper.findAllMusics();
			jedisCluster.set("musics", JSON.toJSONString(musics));
		}
		return musics;
	}
/**
 * 单体缓存
 */
	@Override
	//@Cacheable(value="music",key="'music_'+#musicId")
	public Music findMusicByMusicId(String musicId) {
		return musicMapper.findMusicByMusicId(musicId);
	}

	@Override
	/**
	 * value:主key
	 * key:副key
	 * condition:条件
	 * 单体缓存
	 */
	//@CacheEvict(value="music",key="'music_'+#musicId",condition="#musicId!=null")
	public String deleteMusicByMusicId(String musicId) {
		System.out.println("执行此方法时，当前key从缓存中移除");
		return "删除成功";
	}

	public String testJedisCluster() {
		jedisCluster.set("name", "feifei");
		return jedisCluster.get("name");
		
	}
	
}
